package com.example.demo2.model;

import java.sql.*;

public class DBtest {
    public String name;
    public String pass;

    public void sendInfo(String username,String password){
        this.name = username;
        this.pass = password;
    }

    public String userLogin() {
        String url = "jdbc:mysql://localhost:3306/library_management?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
        String username = "root"; // 你的数据库用户名
        String password = "LQN7582965!"; // 你的数据库密码
        String sql = "select * from user where username=? and password=?";
        String nameChecked="";
        User user = new User();
        if(user!=null){
            red.setAttribute("name",name);
            req.getRequestDispatcher("/success.jsp").forward(req,resp);
        }else {
            req.getRequestDispatcher("/error.jsp").forward(req,resp);
        }



        Connection conn = null;
        PreparedStatement prep = null;
        ResultSet rs = null;



        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, password);


           prep = conn.prepareStatement(sql);
            prep.setString(1, name);
            prep.setString(2, pass);

           rs = prep.executeQuery();



   while(rs.next()){
       user.setId(rs.getInt("id"));
       user.setUsername(rs.getString("username"));
       user.setPassword(rs.getString("password"));
       user.setName(rs.getString("name"));
       user.setRole(rs.getString("role"));
       user.setPhone(rs.getString("phone"));
       user.setEmail(rs.getString("email"));
       user.setCreateTime(
               rs.getTimestamp("create_time")
               .toLocalDateTime()
       );
       user.setStatus(rs.getInt("status"));


   }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);


        }finally{
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                prep.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

    }
}
